package cn.ybk.pet.service.impl;

import cn.ybk.org.domain.Department;
import cn.ybk.pet.domain.PetType;
import cn.ybk.pet.service.IPetTypeService;
import cn.ybk.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {


    @Override
    public List<PetType> tree() {
        ArrayList<PetType> result = new ArrayList<>();
        List<PetType> all = super.findAll();
        Map<Long, PetType> collect = all.stream().collect(Collectors.toMap(PetType::getId, pet -> pet));
        for (PetType petType : all) {
            if (petType.getPid() == null) {
                result.add(petType);
            } else {
                PetType parent = collect.get(petType.getPid());

                if (parent != null) {
                    parent.getChildren().add(petType);
                    System.out.println(parent.getChildren().add(petType));
                }
            }
        }
        return result;
    }
}
